<?php

namespace Admin\Controller;
use Think\Controller;

class AdvertisersController extends Controller{
	

	/*
	列表
	*/

	public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');//当前页数
        $page_size = I('post.page_size/d');

        $ssre=M('advertisers');

        $count = $ssre->where("status=1")->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result=$ssre->order("id desc")->limit($limitStart,$page_size)->where("status=1")->select();

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

    /*展示
    广告主表中的商务
    */

    public function cid_lists(){
        $this->isAjaxLogin();//检查是否登录

        $id = I('get.id');

        $where="";
        if($id!=false){
            $where.="id=$id";
        }

        $model=M('admin_user');

        $arr = M('advertisers')->field('cid')->where($where)->find();

        $result=$model->alias('a')->field('a.name,a.id,a.status')->join('table_user_role  b ON  b.uid=a.id')->join('table_role c ON c.id=b.rid')->where("c.uname='商务' and status=0")->select();
        //print_r($result);die;
         if($result !== false){

            $data['arr'] = $arr;
            $data['data'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }




     /*审核时默选*/
     public function ch_lists(){
        $this->isAjaxLogin();//检查是否登录

        $id=I('post.id');

        $model=M('advertisers');

        $examine_statusone=$model->where("id=$id")->field('examine_status,name,cid')->find();

         if($examine_statusone !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $examine_statusone;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }




    /*
    根据商务id选出对应的广告主
    */

    public function cid_alllists(){
        $this->isAjaxLogin();//检查是否登录
        $page = I('post.page/d');//当前页数
        $page_size = I('post.page_size/d');
        $cid=I('post.user_id');
        $model=M('admin_user');

        
        $where = " b.status=1";
        if($cid != ''){
                $where.=" and b.cid=$cid";
        }

        $count = $model->alias('a')->field('a.name,b.*')->join('table_advertisers  b ON  b.cid=a.id')->where($where)->count();

        $total =  ceil($count / $page_size);
   
        $limitStart = ($page-1) * $page_size;



        $result=$model->alias('a')->field('a.name,b.*')->order('b.id desc')->join('table_advertisers  b ON  b.cid=a.id')->where($where)->limit($limitStart,$page_size)->select();
         if($result !== false ){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

 

    /*
    新增广告主
    */
    public function add(){

        $this->isAjaxLogin();//检查是否登录

        $model=D('advertisers');

        $login_name=I('post.number');//广告主帐号
        $phone=I('post.phone');//广告主公司联系人手机号
        $corporate_name=I('post.corp');//广告主公司名称
        $contacts_name=I('post.name');//广告主公司联系人名字
        $remarks=I('post.content');//备注

        $data['login_name']   = $login_name;
        $data['phone']   = $phone;
        $data['corporate_name'] = $corporate_name;
        $data['contacts_name'] = $contacts_name;
        $data['remarks'] = $remarks;
        $data['insert_time']=time();
        $data['password']='123456';
        $data['examine_status']=0;//0表示未通过
        $data['status']=1;//1表示未锁定，0表示锁定
        if($model->create($data) &&$model->add($data)){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        
        $this->ajaxReturn($ajaxReturn);
    }



	/*
	修改广告主
	*/
	public function save(){

		$this->isAjaxLogin();

		$id=I('post.id/d');
		$login_name=I('post.number');
		$phone=I('post.phone');
		$corporate_name=I('corp');
        //$cid    = I('post.cid');//所属商务id
		$contacts_name=I('post.name');
		$remarks=I('post.content');

		$data['login_name']=$login_name;
		$data['phone']=$phone;
		$data['corporate_name']=$corporate_name;
		$data['contacts_name']=$contacts_name;
		$data['remarks']=$remarks;
        //$data['cid'] = $cid;
		$model=D('advertisers');

		if($model->create($data)){

            $result = $model->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

        }

        

        $this->ajaxReturn($ajaxReturn);
	}

	/*删除*/
    public function delete(){

        $this->isAjaxLogin();//检查是否登录

        $ids = I('get.id');

        $model = M('advertisers');

        $ids = explode(',',$ids);
        $data['status']=0;

        foreach($ids as $id){
            $result=$model-> where('id='.$id)->save($data);
        }
        
        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }


    /*
		审核
    */
    public function review(){
    	$this->isAjaxLogin();//检查是否登录

    	$id     = I('get.id');
        $state  = I('get.radioValue');
        $cid    = I('get.selOpt');
        // $examine_status = I('post.examine_status');

        $Article = M('advertisers');

        $user = M('admin_user')->field("name")->where("id=$cid")->find();
        
        $data['cid'] = $cid;
        $data['examine_status'] = $state;
        $data['name'] = $user['name'];

        $res = $Article-> where('id='.$id)->save($data);
        
        if($res!=false){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "审核成功";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "审核失败";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*
	详情
    */
    public  function infos(){
    	$this->isAjaxLogin();

        $id = I('post.id/d');

        $Model = M('advertisers');

        $result = $Model->where([
        		'id'=>$id,
        	])->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

       //添加银行卡信息
    public function bankcard(){
        $id   = I('get.id');
        $deposit_bank = I('get.deposit_bank');
        $payee= I('get.payee');
        $bank_account = I('get.bank_account');
        $pay_password = I('get.pay_password');

        $data['deposit_bank'] = $deposit_bank;
        $data['payee'] = $payee;
        $data['bank_account'] = $bank_account;
        $data['pay_password'] = $pay_password;

        $advertisers = M('advertisers');
        $result = $advertisers->where('id='.$id)->save($data);

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "添加成功！";

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "添加失败！";

        }

        $this->ajaxReturn($ajaxReturn);

    }

    //查看账户信息
    public function account(){

        $id = I('get.id');

        $advertisers = M('advertisers');

        $result = $advertisers->where('id='.$id)->find();

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查看信息失败！";

        }

        $this->ajaxReturn($ajaxReturn);
    }

    //广告主下单
    public function lower(){

        $page = I('get.page');
        $page_size = I('get.page_size');

        $count = M('lower_order')->count();

        $total =  ceil($count / $page_size);
        
        $limitStart = ($page-1) * $page_size;

        $result = M('lower_order')->order('id desc')->limit($limitStart,$page_size)->select();

         if($result !== false ){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);

    }

    //热门搜索添加
    public function add_search(){
        
        $name = I('post.name');

        $data['name'] = $name;

        $arr = M('search')->select();

        if($arr==null){

            $data['rank'] = 1;
        }

        foreach($arr as $v){
            $data['rank'] = $v['id']+1;
        }

        $result = M('search')->add($data);

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = "SUCCESS";

        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = "添加失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }

    public function delete_search(){

        $id = I('post.id');

        $result = M('search')->where('id='.$id)->delete();

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = "SUCCESS";

        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = "添加失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }

    //展示热门搜索
    public function list_search(){

        $page = I('get.page');
        $page_size = I('get.page_size');

        $count = M('search')->count();

        $total =  ceil($count / $page_size);
        
        $limitStart = ($page-1) * $page_size;

        $result = M('search')->order('rank desc')->limit($limitStart,$page_size)->select();

        if($result !== false ){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);

    }

    //搜索排序
    public function sort_search(){

        $thisid = I('get.id');
        $type   = I('get.type');

        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $searchModel = M('search');

        $thisinfo   = $searchModel->where('id='.$thisid)->getField('rank,id');
        $thisrank   = array_keys($thisinfo)[0];
        $thiscid    = array_values($thisinfo)[0];

        $nextinfo   = $searchModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('id,rank');

        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        if($thisrank && $nextid && $nextrank){

            $searchModel-> where('id='.$thisid)->setField('rank',$nextrank);
            $searchModel-> where('id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }


}